﻿using System.Collections.Generic;
using System.Linq;
using BookShop.DAL;
using BookShop.DTO;
using BookShop.Library.Constant;

namespace BookShop.BLL
{
    public class CategoryService
    {
        public static Category Add(string name)
        {
            using (var repository = new CategoryRepository())
            {
                var existedCategory = GetByName(name);
                if (existedCategory != null)
                {
                    return null;
                }
                return repository.Add(new Category { Name = name, Status = CategoryStatus.Active });
            }
        }

        public static Category Update(int categoryId, string name, int status)
        {
            using (var repository = new CategoryRepository())
            {
                var category = GetById(categoryId);
                if (category != null)
                {
                    category.Name = name;
                    category.Status = status;
                    category = repository.Update(category);
                }
                return category;
            }
        }

        public static bool Delete(int categoryId)
        {
            using (var repository = new CategoryRepository())
            {
                var category = GetById(categoryId);
                if (category == null)
                {
                    return false;
                }
                category.Status = CategoryStatus.Inactive;
                category = repository.Update(category);
                return category != null;
            }
        }

        public static List<Category> GetAll()
        {
            using (var repository = new CategoryRepository())
            {
                return repository.FindAll(c => c.Status == CategoryStatus.Active).OrderBy(c => c.Name).ToList();
            }
        }

        public static Category GetById(int categoryId)
        {
            using (var repository = new CategoryRepository())
            {
                return repository.Find(c => c.Id == categoryId);
            }
        }

        public static Category GetByName(string name)
        {
            using (var repository = new CategoryRepository())
            {
                return repository.Find(c => c.Name.Equals(name.Trim().ToLower()));
            }
        }

        public static List<Category> GetByStatus(int status)
        {
            using (var repository = new CategoryRepository())
            {
                return repository.FindAll(c => c.Status == status).ToList();
            }
        }

        public static List<Category> Search(int? categoryId, string name, int? status, int page, int pageSize, out int count)
        {
            var repository = new CategoryRepository();
            var returnList = new List<Category>();
            if (categoryId.HasValue)
            {
                var category = GetById(categoryId.Value);
                if (category != null)
                {
                    returnList.Add(category);
                }
            }
            else
            {
                returnList = repository.GetAll().ToList();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    returnList = returnList.FindAll(c => c.Name.ToLower().Contains(name.Trim().ToLower()));
                }
                if (status.HasValue)
                {
                    returnList = returnList.FindAll(c => c.Status == status.Value);
                }
            }
            count = returnList.Count;
            return returnList.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }
    }
}
